import {create} from 'zustand'
import {devtools, persist} from 'zustand/middleware'

const authKey = `${import.meta.env.VITE_APP_AUTH_KEY}`;

interface Account {
    jwt?: string
    userName?: string,
    avatar?: string,
    isLogin?: boolean,
    setLogin: (jwt: string) => void,

    logOut: () => void,

}

export const useAccountStore = create<Account>()(
    devtools(
        persist(
            (set) => ({
                jwt: '',
                userName: '',
                avatar: '',
                isLogin: false,
                setLogin: (jwt: string) => set((state) => {
                    localStorage.setItem(authKey || "authKey", jwt);
                    return ({isLogin: true, jwt: jwt});
                }),
                logOut: () => set((state) => ({isLogin: false, jwt: ''})),
            }),
            {
                name: 'account-storage',
            }
        )
    )
)